package com.ptu.mapper;

import com.ptu.entity.Cart;
import com.ptu.vo.CartVO;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface CartMapper {

    // 根据productId查询购物车
    @Select("select * from cart where product_id = #{productId}")
    Cart getByProductId(int productId);

    // 更新购物车商品数量
    @Update("update cart set quantity = quantity + 1, update_time = #{updateTime} where product_id = #{productId}")
    void addQuantity(int productId, LocalDateTime updateTime);

    // 获取购物车列表
    @Select("select * from cart where user_id = #{userId}")
    List<CartVO> getCartList(int userId);

    @Insert("insert into cart (user_id, product_id, quantity, unit_price, create_time, update_time) values (#{userId}, #{productId}, #{quantity}, #{unitPrice}, #{createTime}, #{updateTime})")
    void insert(Cart cart);

    @Delete("delete from cart where user_id = #{userId} and product_id = #{productId}")
    void deleteCart(int userId, Integer productId);

    @Delete("delete from cart where user_id = #{userId}")
    void clearCart(int userId);

    @Select("select * from cart where user_id = #{userId} and product_id = #{productId}")
    Cart getById(int userId, int productId);

    @Update("update cart set quantity = quantity - 1, update_time = #{updateTime} where user_id = #{userId} and product_id = #{productId}")
    void subtractQuantity(int userId, Integer productId, LocalDateTime updateTime);

    void batchDelete(List<Integer> productIds, int userId);

    @Update("update cart set quantity = quantity - 1, update_time = #{updateTime} where user_id = #{userId} and product_id = #{productId}")
    void subtractCart(int userId, Integer productId);

    @Delete("delete from cart where user_id = #{userId}")
    void deleteByUserId(Integer userId);
}
